#include <Arduino.h>
#include "BreathingLamp.h"
#include "DBManger.h"
#include "FishControl.h"
#include "HttpManger.h"
#include "MainManger.h"
#include "StatusLamp.h"
#include "AliIoTControl.h"
#include <TimeLib.h>
#include <Ticker.h>
#include <ESPAsyncWiFiManager.h>
#include "temperature/Temperature.h"

#define BLN_PIN GPIO_ID_PIN(4)
#define PWM0_PIN GPIO_ID_PIN(14)
#define PWM1_PIN GPIO_ID_PIN(12)
#define CTRL_PIN GPIO_ID_PIN(13)

MainManger mainManger;
DBManger dbManger;
BreathingLamp breathingLamp(BLN_PIN);
StatusLamp stLamp(LED_BUILTIN);
FishControl fishCtrl(CTRL_PIN, PWM0_PIN, PWM1_PIN);
HttpManger httpManger(80);
AliIoTControl aliIotControl;
Temperature temper;

AsyncWiFiManager wifiManager(httpManger.webServer(), httpManger.dns());

Ticker ticker;

void setup()
{
  Serial.begin(74800);
  ticker.attach(0.1, []() {
    stLamp.Run();
    fishCtrl.Run();
    temper.Run();
  });

  stLamp.setSt(StatusLamp::_ST_LOOKUP_NETWORK);
  dbManger.init();
  fishCtrl.init();

  wifiManager.setAPCallback([](AsyncWiFiManager *wifiManager) {
    Serial.println("Entered config mode");
    Serial.println(WiFi.softAPIP());
    //if you used auto generated SSID, print it
    Serial.println(wifiManager->getConfigPortalSSID());
    //entered config mode, make led toggle faster
    stLamp.setSt(StatusLamp::_ST_WIFI_CONFIG);
  });

  if (!wifiManager.autoConnect("FISH_TANK_AP"))
  {
    ESP.reset();
    delay(1000);
  }
  ticker.detach();
  httpManger.begin();
  mainManger.init();
}

void loop()
{
  mainManger.loop();
  breathingLamp.Run();
  stLamp.Run();
  fishCtrl.Run();
  temper.Run();
}